﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class OCompletingCheckDAL
    {
        public static bool Add(OCompletingCheckInfo obj, List<ONGInspectionInfo> NGItems)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_Add",obj.SerialNumber,obj.MachineType,obj.CheckDate,obj.CreateDate,obj.EngineNumber,obj.OperationMode, obj.Cab,obj.Track,obj.Bucket,obj.Platform,obj.FrameUnder,obj.Forearm,obj.TeShuPeiGuan,obj.GPSNumber,obj.TuiTuBan,obj.HeGe,obj.BengNum,obj.DuoLuFaNum,obj.HuiZhuanMaDaNum,obj.GunPanNum,obj.ZYHZJTNum,obj.YYYXBHNum,obj.RYXBHNum,obj.XZMDZNum,obj.XZMDYNum,obj.TTBCYLZNum,obj.TTBCYLYNum,obj.DBCYLNum,obj.XBCYLNum,obj.CDCYLNum,obj.DBBHNum,obj.XBBHNum,obj.YouWen,obj.ShuiWen,obj.YYYQJD,obj.ZDZS,obj.QDQDJItem,obj.XNJCItem,obj.GZQRItem,obj.CheckMan,obj.KTBHNum,obj.DBCYLNumRight);
            try
            {

                int fromid = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                //机号已存在
                if (fromid <= 0)
                {
                    return false;
                }
                foreach (ONGInspectionInfo item in NGItems)
                {
                    item.FromId = fromid;
                    OInspectionDAL.AddNGItem(item);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Delete(int CCID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_Delete", CCID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetBySerialNumber(string SerialNumber)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_GetBySerialNumber", SerialNumber);
            return DB.ExecuteReader(dbCommand);
	    }

        public static IDataReader GetById(int id)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_GetById", id);
            return DB.ExecuteReader(dbCommand);
        }


        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
				            DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt16( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }


        public static DataTable OCompletingCheck_ExportNGListByDate(string FormDate, string ToDate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_ExportNGListByDate", FormDate, ToDate);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        /// <summary>
        /// 判断完成检查书中，相关号码是否已存在
        /// </summary>
        /// <returns></returns>
        public static int OCompletingCheck_GetCheckUniqueNum(string ColumnName, string ColumnValue)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_GetCheckUniqueNum", ColumnName, ColumnValue);
            return Convert.ToInt32( DB.ExecuteScalar(dbCommand));
        }
    }
}
